package com.nwpu.vote.model;

import java.util.ArrayList;
import java.util.Date; 
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy; 
import javax.jdo.annotations.IdentityType; 
import javax.jdo.annotations.PersistenceCapable; 
import javax.jdo.annotations.Persistent; 
import javax.jdo.annotations.PrimaryKey; 

import com.nwpu.vote.model.Vote;
import com.nwpu.vote.util.PMF;

import com.google.appengine.api.datastore.Key;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Tag {
	// content mapping ------------------------------------------------
	@PrimaryKey 
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY) 
    private Key key;
 
    @Persistent 
    private String name;
        
    @Persistent 
    private String description;
 
    @Persistent 
    private Set<Key> taggedVoteKeys = new HashSet<Key>();
    
    public Tag(String name, String description) { 
        this.name = name; 
        this.description = description;
    } 
 
    // get and set ----------------------------------------------------
    public Key getKey() {
		return key;
	}
 
    public String getName() { 
        return name; 
    } 
 
    public String getDescription() { 
        return description; 
    }
    
    public Set<Key> getTaggedVoteKeys() {
		return taggedVoteKeys;
	}
    
    public void setKey(Key key) {
		this.key = key;
	}
    
    public void setName(String name) { 
        this.name = name; 
    } 
    
    public void setDescription(String description) { 
        this.description = description; 
    } 
 
    public void addTaggedVoteKey(Vote vote){
		this.taggedVoteKeys.add(vote.getKey());
	}
    
    public List<Vote> getTaggedVotes(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<Vote> votes = new ArrayList<Vote>();
		
		if(this.taggedVoteKeys != null){
			for(Key key: this.taggedVoteKeys){
				Vote vote = pm.getObjectById(Vote.class, key);
				votes.add(vote);
			}
		}
		
		return votes;
	}
}
